package cn.itcast.item.service.impl;

import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.hmall.pojo.item.ItemDoc;
import cn.itcast.item.mapper.ItemMapper;
import cn.itcast.item.service.ItemService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;


import java.io.IOException;
import java.util.List;
@SpringBootTest
public class test {
    RestHighLevelClient client;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemService itemService;

    @Test
    public void find1() throws IOException {
        client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://120.48.83.4:9200")
        ));
        for (int i = 1; i < 21; i++) {

            IPage<Item> page = new Page(i, 5);
            QueryWrapper<Item> wrapper = new QueryWrapper<>();

            itemMapper.selectPage(page, null);
            List<Item> records = page.getRecords();//当前页数据
            long total = page.getTotal();//总条数
//            System.out.println(pageDTO);
            System.out.println(total);
            BulkRequest request = new BulkRequest("item");
            for (Item record : records) {
                IndexRequest request1 = new IndexRequest();
                request1.id(record.getId() + "");
                ItemDoc itemDoc = new ItemDoc(record);
                request1.source(JSON.toJSONString(itemDoc), XContentType.JSON);
                request.add(request1);
            }
            BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
            System.out.println(bulk.status());
        }
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}